package cn.lijiguo.concurrency.part1.threadobjectmethods;

/**
 * SleepDontReleaseMonitor
 * 展示 线程 sleep 的时候不释放 synchronized 的 monitor
 *  等 sleep 的时间到了之后，正常结束后才会释放锁
 * @author lijiguo
 * @date 2020/1/7 13:28
 */
public class SleepDontReleaseMonitor implements Runnable{

    public static void main(String[] args) {
        SleepDontReleaseMonitor r = new SleepDontReleaseMonitor();

        new Thread(r, "t1").start();
        new Thread(r, "t2").start();
    }

    @Override
    public void run() {
        sync();
    }

    private synchronized void sync() {
        System.out.println(Thread.currentThread().getName() + " 获取到了 Monitor");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName() + " 退出了同步代码块");
    }
}
